﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Display {
    import flash.events.*;
    import Engine.Managers.*;
    import flash.display.*;
    import flash.geom.*;
    import Events.*;
    import Display.DialogUI.*;
    import Classes.*;
    import Transactions.*;
    import Classes.util.*;
    import Engine.*;

    public class MissionPopup extends SWFDialog {

        private static const AVATAR_SCALE:Number = 1;
        public static const COUNTER:String = "missionPopup";

        private var m_window:MovieClip;
        protected var m_missionObjs:Array;
        protected var m_mission:Mission;
        private var m_greeting:String;
        private var m_greetingText:String;
        private var m_missionHost:Player;
        private var m_accept:GenericButton;
        private var m_missionType:String;
        private var m_callback:Function;

        public function MissionPopup(_arg1:Mission, _arg2:String=""){
            this.m_missionObjs = [];
            SoundManager.addSound(_arg1.missionType, _arg1.soundURL, false);
            m_dialogAsset = "assets/dialogs/FV_FriendMission.swf";
            super();
            if (!_arg1.hostPlayer){
                this.m_missionHost = Global.player.findFriendById(_arg1.missionHostId);
            } else {
                this.m_missionHost = _arg1.hostPlayer;
            };
            if (!this.m_missionHost){
                throw (new Error("Host of mission must be a friend of the player"));
            };
            this.m_mission = _arg1;
            this.m_missionType = _arg2;
        }
        private function addFacebookPic():void{
            var _local1:DisplayObject;
            var _local2:String;
            if (((this.m_missionHost) && (this.m_missionHost.snUser))){
                _local2 = this.m_missionHost.snUser.picture;
                if (_local2){
                    _local1 = LoadingManager.load(_local2);
                };
            };
            if (_local1 == null){
                _local1 = new EmbeddedArt.hud_no_profile_pic();
                _local1.width = 48;
                _local1.height = 48;
            };
            this.m_window.helpfriend_mc.fbImage_mc.addChild(_local1);
        }
        override protected function onLoadComplete():void{
            var _local1:Array = [];
            this.m_window = (m_loader.content as MovieClip);
            this.m_accept = new GenericButton(this.m_window.helpfriend_mc.accept_bt, this.onAccept);
            this.m_accept.text = ZLoc.t("Dialogs", "Accept");
            this.m_window.helpfriend_mc.close_bt.addEventListener(MouseEvent.CLICK, this.onCancel);
            this.setGreeting(this.m_greeting);
            this.addFacebookPic();
            this.count("view_initial");
            addChild(this.m_window);
            if (Global.isVisiting()){
                StatsManager.milestone("social_miss_md");
            };
        }
        private function count(_arg1:String, _arg2:String=null):void{
            var _local3:String = MissionPopup.COUNTER;
            if (this.m_missionType != ""){
                _local3 = (_local3 + ("_" + this.m_missionType));
            };
            if (_arg2 != null){
                StatsManager.count(_local3, this.m_mission.missionType, _arg1, _arg2);
            } else {
                StatsManager.count(_local3, this.m_mission.missionType, _arg1);
            };
        }
        protected function onAccept(_arg1:MouseEvent):void{
            this.m_callback(true);
            this.count("click_accept");
        }
        protected function onCancel(_arg1:MouseEvent):void{
            this.m_callback(false);
            this.count("click_cancel");
        }
        private function setGreeting(_arg1:String):void{
            this.m_greeting = _arg1;
            if (((!((this.m_window == null))) && (_arg1))){
                this.m_window.helpfriend_mc.greeting_tf.text = _arg1;
                this.m_window.helpfriend_mc.playerName_tf.text = this.m_missionHost.firstName;
                Global.missionHostFirstName = this.m_missionHost.firstName;
            };
        }
        public function offerIdleMission():void{
            var _local1:String = ZLoc.t("Main", "Mission_Idle_Prompt", {
                name:Global.player.firstName,
                fname:this.m_missionHost.firstName
            });
            this.setGreeting(_local1);
            this.m_callback = this.onIdleMission;
        }
        protected function onIdleMission(_arg1:Boolean):void{
            if (_arg1){
                this.close();
                UI.visitNeighbor(this.m_missionHost.snUser.uid);
            } else {
                this.close();
            };
        }
        public function startMission():void{
            var _local1:String = ZLoc.t("Missions", (this.m_mission.missionType + "_description"), {
                name:ZLoc.tn(Global.player.firstName),
                fname:this.m_missionHost.firstName
            });
            this.setGreeting(_local1);
            this.m_callback = this.onConfirmMission;
            this.createMissionObjects();
        }
        private function onConfirmMission(_arg1:Boolean):void{
            if (_arg1){
                this.completeMission();
            } else {
                this.close();
            };
        }
        private function completeMission():void{
            var _local3:String;
            var _local4:Player;
            var _local5:GenericDialog;
            SoundManager.playSound(this.m_mission.missionType);
            GameTransactionManager.addTransaction(new TCompleteMission(this.m_mission.missionType), true);
            var _local1:int = Global.gameSettings.getInt("missionXp");
            var _local2:int = Global.gameSettings.getInt("missionGold");
            if (this.m_mission.hitLimit){
                _local1 = Global.gameSettings.getInt("missionXpLimit");
                _local2 = Global.gameSettings.getInt("missionGoldLimit");
            };
            Global.player.xp = (Global.player.xp + _local1);
            Global.player.gold = (Global.player.gold + _local2);
            this.removeMissionObjects();
            if (GlobalEngine.getFlashVar("giftMission") == null){
                _local3 = ZLoc.t("Main", "Mission_Thank_You");
                this.setGreeting(_local3);
                this.m_callback = this.onOKMessage;
                this.hideCancelButton();
            } else {
                this.close();
                _local4 = this.m_mission.hostPlayer;
                _local3 = ZLoc.t("Main", "Mission_Thank_You_Gift", {name:ZLoc.tn(_local4.firstName)});
                _local5 = new GenericDialog(_local3, "", GenericDialogView.TYPE_SENDGIFTS, this.playerInputHandler);
                UI.displayPopup(_local5);
            };
            if (Global.isVisiting()){
                StatsManager.milestone("social_miss_ty_md");
            };
        }
        protected function playerInputHandler(_arg1:GenericPopupEvent):void{
            if (_arg1.button == GenericPopup.ACCEPT){
                GlobalEngine.socialNetwork.redirect("gifts.php?ref=missionDialogDone");
            };
        }
        private function hideCancelButton():void{
            this.m_window.helpfriend_mc.close_bt.visible = false;
        }
        private function onOKMessage(_arg1:Boolean):void{
            this.close();
        }
        override public function close():void{
            dispatchEvent(new CloseEvent(CloseEvent.CLOSE));
            super.close();
        }
        private function createMissionObjects():void{
            var _local3:MissionObject;
            var _local4:Number;
            var _local5:Number;
            var _local1:int = this.m_mission.numObjects;
            var _local2:int;
            while (_local2 < _local1) {
                _local3 = new MissionObject(this.m_mission);
                _local4 = int(Utilities.randBetween(4, (Global.world.getGridWidth() - 4)));
                _local5 = int(Utilities.randBetween(4, (Global.world.getGridHeight() - 4)));
                _local3.setPosition(_local4, _local5);
                _local3.setOuter(Global.world);
                _local3.attach();
                this.m_missionObjs.push(_local3);
                _local2++;
            };
        }
        private function removeMissionObjects():void{
            var _local1:Array = this.m_missionObjs;
            var _local2:int;
            while (_local2 < _local1.length) {
                _local1[_local2].detach();
                _local2++;
            };
            this.m_missionObjs = [];
        }
        public function getMissionObjects():Array{
            return (this.m_missionObjs);
        }
        override public function getDialogOffset():Point{
            return (new Point((((-(Global.stage.stageWidth) / 2) + (this.width / 2)) + 40), (((-(Global.stage.stageHeight) / 2) + (this.height / 2)) + 40)));
        }

    }
}//package Display 
